using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;

namespace CSSg2Help
{
    public partial class Sg2Help : Form
    {
        private AccountList list = null;
        public static Trace trace;
        private int posIndex = 0;
        private int time = 0;
        public Sg2Help()
        {
            InitializeComponent();
        }

        private void Sg2Help_Load(object sender, EventArgs e)
        {
            RegisterHotKey(this.Handle, 200, (int)MyKeys.None, (int)Keys.F5);
            RegisterHotKey(this.Handle, 201, (int)MyKeys.None, (int)Keys.F6);
            string path = Application.StartupPath;
            list = new AccountList(path + "//AccountList.txt");
            for (int i = 0; i < list.list.Count; i++)
            {
                TreeNode[] tn = tvAccount.Nodes.Find(list.list[i].parent, false);
                if (tn.Length > 0)
                {
                    tn[0].Nodes.Add(list.list[i].accountName);
                }
                else
                {
                    tvAccount.Nodes.Add(list.list[i].parent, list.list[i].parent);
                    i--;
                }
            }
            if (tvAccount.Nodes.Count>0)
            {
                if (tvAccount.Nodes[0].Nodes.Count > 0)
                {
                    tvAccount.SelectedNode = tvAccount.Nodes[0].Nodes[0];
                }
                else
                {
                    tvAccount.SelectedNode = tvAccount.Nodes[0];
                }
            }

            timer1.Interval = 1000;
            timer1.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tvAccount.SelectedNode.Parent==null)
            {
                return;
            }
            Account account = list.GetAccountByName(tvAccount.SelectedNode.Text);
            Http h = new Http(account.host, account.accountName, account.password, account.serverId);
            System.Diagnostics.Process.Start(h.GetUrl());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (tvAccount.SelectedNode != tvAccount.SelectedNode.Parent.LastNode)
            {
                tvAccount.SelectedNode = (tvAccount.SelectedNode.NextNode.Parent == null) ? tvAccount.SelectedNode.NextNode.NextNode : tvAccount.SelectedNode.NextNode;
            }
            else
            {
                if (tvAccount.SelectedNode.Parent != null && tvAccount.SelectedNode.Parent.NextNode != null)
                {
                    tvAccount.SelectedNode = tvAccount.SelectedNode.Parent.NextNode.Nodes[0];
                }
            }
            CurrentAccount.Text = tvAccount.SelectedNode.Text;
        }

        private void tvAccount_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tvAccount.SelectedNode != null && tvAccount.SelectedNode.Parent != null)
            {
                CurrentAccount.Text = tvAccount.SelectedNode.Text;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button2_Click(sender, e);
            button1_Click(sender, e);
        }
        private enum MyKeys
        {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4,
            Win = 8
        }
        const int WM_HOTKEY = 0x312;
        [DllImportAttribute("user32.dll", EntryPoint = "RegisterHotKey")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
        [DllImportAttribute("user32.dll", EntryPoint = "UnregisterHotKey")]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_HOTKEY)
            {
                switch (m.WParam.ToInt32())
                {
                    case 200:
                        button3_Click(this, EventArgs.Empty);
                        break;
                    case 201:
                        btnClickTrace_Click(this, EventArgs.Empty);
                        break;
                }
            }
            base.WndProc(ref m);
        }

        private void Sg2Help_FormClosed(object sender, FormClosedEventArgs e)
        {
            UnregisterHotKey(this.Handle, 200);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            TraceSetting ts = new TraceSetting();
            ts.ShowDialog();
        }

        private void btnClickTrace_Click(object sender, EventArgs e)
        {
            if (trace == null) return;
            if (posIndex >= trace.posList.Count) posIndex = 0;
            int x = trace.posList[posIndex].x;
            int y = trace.posList[posIndex].y;
            MouseEvent.mouse_click(x, y);
            posIndex += 1;
            label2.Text = posIndex.ToString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int h = 0;
            int m = 0;
            int s = 0;
            time++;
            s = time % 60;
            m = (time - s) / 60 % 60;
            h = (time - s - m * 60) / 3600;
            label1.Text = ((h < 10) ? "0" + h : h.ToString()) + ":" + ((m < 10) ? "0" + m : m.ToString()) + ":" + ((s < 10) ? "0" + s : s.ToString());
        }
    }
}